package com.mapapp.flutter_mapapp.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mapapp.flutter_mapapp.R;
import com.mapapp.flutter_mapapp.bean.ProcessBean;
import com.mapapp.flutter_mapapp.bean.StepFloorProcessBean;
import com.mapapp.flutter_mapapp.widget.CustomizedRoundProgressBar;

import java.text.DecimalFormat;
import java.util.List;

public class MapStepSearchAdapter extends RecyclerView.Adapter<MapStepSearchAdapter.ClockShopViewHolder>{

    private List<StepFloorProcessBean> mSteps;
    private Context mContext;

    public MapStepSearchAdapter(List<StepFloorProcessBean> steps , Context content){
        this.mSteps = steps;
        this.mContext = content;
    }

    public void notify(List<StepFloorProcessBean> steps){
        this.mSteps = steps;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ClockShopViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_map_step, parent, false);
        ClockShopViewHolder holder = new ClockShopViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ClockShopViewHolder holder, int position) {
        String[] colors = mContext.getResources().getStringArray(R.array.stepcolor);
        StepFloorProcessBean bean = mSteps.get(position);
        holder.order.setText(bean.getName());
        DecimalFormat df = new DecimalFormat("######0.00");
        holder.percent.setText(df.format(bean.getPercent()*100)+"%");
        if(position < 5){
            holder.percent.setTextColor(Color.parseColor(colors[position]));
            holder.bar.setGradualColor(Color.parseColor(colors[position]));
        }else{
            holder.percent.setTextColor(Color.parseColor(colors[5]));
            holder.bar.setGradualColor(Color.parseColor(colors[5]));
        }
        holder.bar.setCurrentCount((float) bean.getPercent()*100);
    }

    @Override
    public int getItemCount() {
        if (mSteps == null) {
            return 0;
        }
        return mSteps.size();
    }

    static class ClockShopViewHolder extends RecyclerView.ViewHolder {
        private TextView order;
        private TextView percent;
        private CustomizedRoundProgressBar bar;

        public ClockShopViewHolder(View itemView) {
            super(itemView);
            order = itemView.findViewById(R.id.tv_item_map_step_order);
            percent = itemView.findViewById(R.id.tv_item_map_step_percent);
            bar = itemView.findViewById(R.id.cpb_item_map_step_process);
        }
    }

}
